package com.example.cafe.work.controller;


import com.example.cafe.work.dto.OrderDisplay;
import com.example.cafe.work.dto.Result;
import com.example.cafe.work.model.Order;
import com.example.cafe.work.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-09-16
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {
    @Autowired
    UserinfoService userinfoService;

    //充值，传金额,前端加验证输入的合法性
    @RequestMapping("/add")
    public Result Recharge(@RequestParam("userId") String userId, @RequestParam("money") int money){
        return userinfoService.Recharge(userId, money);
    }
   /*1，先查询余额,然后查询订单表的total
    2，两者进行比较，余额不足则返回，足够则直接扣减
    3，扣减完成后对订单表的状态进行修改
    4，加事务保证可以回滚
    */
    @RequestMapping("/consume")
    public Result Consume(@RequestBody List<Order> orders){
        return userinfoService.Consume(orders);
    }
    @RequestMapping("/chargeback")
    public Result ChargeBack(@RequestBody List<OrderDisplay> orders){
        return  userinfoService.ChargeBack(orders);
    }


}

